import 'package:http/http.dart' as http;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:convert';

class ServiceAccessSubmit extends StatelessWidget {
  final TextEditingController appKeyController = TextEditingController(text: 'mh_83c9393fcfd7b889');
  final TextEditingController vController = TextEditingController(text: '1.0');
  final TextEditingController signMethodController = TextEditingController(text: 'MD5');
  final TextEditingController signController = TextEditingController(text: '123456');
  final TextEditingController businessNoController = TextEditingController();
  final TextEditingController typeController = TextEditingController(text: '1');
  final TextEditingController businessTypeController = TextEditingController(text: '9610');
  final TextEditingController businessNameController = TextEditingController(text: '2024052802批次零售出口');
  final TextEditingController ebcNameController = TextEditingController(text: '广州伟星电子商务有限公司');
  final TextEditingController tradingMallLinkController = TextEditingController(text: 'https://cbec-mall.gzport.net/');
  final TextEditingController ebpNameController = TextEditingController(text: 'CHEEHON CO.,LTD');
  final TextEditingController platformBuyerController = TextEditingController(text: 'CHEEHON CO.,LTD');
  final TextEditingController supplyNameController = TextEditingController(text: '东莞伟星服装厂');
  final TextEditingController declareNameController = TextEditingController(text: '广州伟星电子商务有限公司');
  final TextEditingController logisticsNameController = TextEditingController(text: '广州伟星运输有限公司');
  final TextEditingController transportNameController = TextEditingController(text: '深圳市货拉拉运输有限公司');
  final TextEditingController transportAbroadNameController = TextEditingController(text: '深伟星国际货运代理有限公司');
  final TextEditingController payNameController = TextEditingController(text: '招商银行股份有限公司');

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('出口业务接入-提交'),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Column(
          children: <Widget>[
            TextField(
              controller: appKeyController,
              decoration: InputDecoration(hintText: 'mh_83c9393fcfd7b889'),
            ),
            TextField(
              controller: vController,
              decoration: InputDecoration(hintText: '1.0'),
            ),
            TextField(
              controller: signMethodController,
              decoration: InputDecoration(hintText: 'MD5'),
            ),
            TextField(
              controller: signController,
              decoration: InputDecoration(hintText: '123456'),
            ),
            TextField(
              controller: businessNoController,
              decoration: InputDecoration(hintText: '请输入businessno'),
            ),
            TextField(
              controller: typeController,
              decoration: InputDecoration(hintText: '1'),
            ),
            TextField(
              controller: businessTypeController,
              decoration: InputDecoration(hintText: '9610'),
            ),
            TextField(
              controller: businessNameController,
              decoration: InputDecoration(hintText: '2024052802批次零售出口'),
            ),
            TextField(
              controller: ebcNameController,
              decoration: InputDecoration(hintText: '广州伟星电子商务有限公司'),
            ),
            TextField(
              controller: tradingMallLinkController,
              decoration: InputDecoration(hintText: 'https://cbec-mall.gzport.net/'),
            ),
            TextField(
              controller: ebpNameController,
              decoration: InputDecoration(hintText: 'CHEEHON CO.,LTD'),
            ),
            TextField(
              controller: platformBuyerController,
              decoration: InputDecoration(hintText: 'CHEEHON CO.,LTD'),
            ),
            TextField(
              controller: supplyNameController,
              decoration: InputDecoration(hintText: '东莞伟星服装厂'),
            ),


            ElevatedButton(
              child: Text('上传'),
              onPressed: () async {
                if (appKeyController.text.isEmpty ||
                    vController.text.isEmpty ||
                    signMethodController.text.isEmpty ||
                    signController.text.isEmpty) {
                  showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text('错误'),
                        content: Text('请填写所有必要的信息。'),
                        actions: <Widget>[
                          TextButton(
                            child: Text('确定'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                        ],
                      );
                    },
                  );
                  return;
                }

                // 构建请求数据
                Map<String, dynamic> requestData = {
                  'app_key': appKeyController.text,
                  'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
                  'v': vController.text,
                  'sign_method': signMethodController.text,
                  'sign': signController.text,
                  'data': {
                    "service_info": {
                      "o_business_no": businessNoController.text,
                      "type": typeController.text,
                      "o_business_type": businessTypeController.text,
                      "o_business_name": businessNameController.text,
                      "o_ebc_name": ebcNameController.text,
                      "o_trading_mall_link": tradingMallLinkController.text,
                      "o_ebp_name": ebpNameController.text,
                      "o_platform_buyer": platformBuyerController.text,
                      "o_supply_name": supplyNameController.text,
                      "o_declare_name": declareNameController.text,
                      "o_logistics_name": logisticsNameController.text,
                      "o_transport_name": transportNameController.text,
                      "o_transport_abroad_name": transportAbroadNameController.text,
                      "o_pay_name": payNameController.text
                    },
                    "service_base_info": {
                      "o_export_contract": "ECK202405280002",
                      "o_deal_curr": "美元",
                      "o_price_term": "FOB",
                      "o_freight_fee": "0",
                      "o_insurance_fee": "0",
                      "o_settle_date": "2024-05-31",
                      "o_ebc_name": "广州伟星电子商务有限公司",
                      "o_ebc_ename": "WEIXIN",
                      "o_ebc_contact": "Weixing Zhang",
                      "o_ebc_contact_tell": "+86-18620202525",
                      "o_ebc_address": "广州市海珠区仑头路21号",
                      "o_ebc_eaddress": "21 Luntou Road, Haizhu District, Guangzhou City",
                      "o_plat_name": "CHEEHON CO.,LTD",
                      "o_plat_ename": "CHEEHON CO.,LTD",
                      "o_plat_contact": "SUSAN",
                      "o_plat_contact_tell": "001-202-495-2266",
                      "o_plat_address": "",
                      "o_plat_eaddress": "3505 International Place,N.W. Washington,D.C.20008 U.S.A.",
                      "o_special": "0",
                      "o_ship_date": "2024-05-27",
                      "o_pack_way": "4",
                      "o_ship_way": "4",
                      "o_ship_port": "广州",
                      "o_country": "美国",
                      "o_country_port": "华盛顿",
                      "o_export_note": ""
                    },
                    "order_info": [
                      {
                        "o_order_no": "20240528000002",
                        "o_pay_no": "P20240528000002",
                        "o_waybill_no": "SF20240528000002",
                        "o_charge": "100",
                        "o_goods_value": "100",
                        "o_currency": "502",
                        "o_accounting_date": "20240528121312",
                        "o_traf_mode": 5,
                        "o_country": "502",
                        "o_order_note": "",
                        "order_goods_info": [
                          {
                            "o_classification": "衣服",
                            "o_goods_ename": "dress",
                            "o_goods_cname": "连衣裙",
                            "o_goods_sku": "SP00001",
                            "o_goods_num": "1",
                            "o_goods_price": "100",
                            "o_goods_total": 100,
                            "o_goods_link": "http:// xxxx.com/....",
                            "o_goods_enterprice": "广州市XXX服装有限公司",
                            "o_goods_ingrediant": "棉100%",
                            "o_goods_note": ""
                          }
                        ]
                      }
                    ]
                  }
                };

                // 发送请求
                http.Response response = await http.post(
                  Uri.parse('https://cbec-studapi.gzport.net/api/access_service/apply_service'),
                  headers: <String, String>{
                    'Content-Type': 'application/json; charset=UTF-8',
                  },
                  body: jsonEncode(requestData),
                );

                if (response.statusCode == 200) {
                  print('上传成功');
                  Map<String, dynamic> responseData = jsonDecode(response.body);
                  showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text('上传成功'),
                        content: SingleChildScrollView(
                          child: ListBody(
                            children: <Widget>[
                              Text('响应状态: ${responseData['status']}'),
                              Text('响应代码: ${responseData['code']}'),
                              Text('响应信息: ${responseData['message']}'),
                              Text('响应时间: ${responseData['time']}'),
                              Text('企业用户id: ${responseData['app_key']}'),
                              Text('签名算法: ${responseData['sign_method']}'),
                              Text('加密信息: ${responseData['sign']}'),
                              Text('响应数据: ${responseData['data']}'),
                            ],
                          ),
                        ),
                        actions: <Widget>[
                          TextButton(
                            child: Text('确定'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                        ],
                      );
                    },
                  );
                } else {
                  print('上传失败');
                  showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text('上传失败'),
                        content: SingleChildScrollView(
                          child: ListBody(
                            children: <Widget>[
                              Text('响应状态: ${response.statusCode}'),
                              Text('响应信息: ${response.reasonPhrase}'),
                            ],
                          ),
                        ),
                        actions: <Widget>[
                          TextButton(
                            child: Text('确定'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                        ],
                      );
                    },
                  );
                }
              },
            ),
          ],
        ),
      ),
    );
  }
}